<!DOCTYPE html>
<html  lang="zh">
<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8" />

<meta name="generator" content="Hexo 3.9.0" />

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />

<title>shader学习8-多种光照 - 游戏人生</title>


    <meta name="description" content="shade学习">
<meta name="keywords" content="学习">
<meta property="og:type" content="article">
<meta property="og:title" content="shader学习8-多种光照">
<meta property="og:url" content="http://cloundgame.gitee.io/mystudy/2020/06/04/shader学习8-多种光照/index.html">
<meta property="og:site_name" content="游戏人生">
<meta property="og:description" content="shade学习">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://cloundgame.gitee.io/mystudy/images/og_image.png">
<meta property="og:updated_time" content="2020-06-06T08:28:48.923Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="shader学习8-多种光照">
<meta name="twitter:description" content="shade学习">
<meta name="twitter:image" content="http://cloundgame.gitee.io/mystudy/images/og_image.png">







<link rel="icon" href="/mystudy/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.9.0/css/all.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/atom-one-dark.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/mystudy/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/mystudy/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/mystudy/css/style.css"><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body class="is-3-column">
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/mystudy/">
            
                <img src="/mystudy/images/logo.svg" alt="shader学习8-多种光照" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/mystudy/">主页</a>
                
                <a class="navbar-item"
                href="/mystudy/archives">日志</a>
                
                <a class="navbar-item"
                href="/mystudy/categories">分类</a>
                
                <a class="navbar-item"
                href="/mystudy/">关于</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="Download on GitHub" href="https://github.com/koliy/koliy.github.io">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                <a class="navbar-item is-hidden-tablet catalogue" title="目录" href="javascript:;">
                    <i class="fas fa-list-ul"></i>
                </a>
                
                
                <a class="navbar-item search" title="搜索" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-6-widescreen has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2020-06-04T06:45:57.000Z">2020-06-04</time>
                
                <div class="level-item">
                <a class="has-link-grey -link" href="/mystudy/categories/shade学习/">shade学习</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    
                    
                    23 分钟 读完 (大约 3433 个字)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span>次访问
                </span>
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                shader学习8-多种光照
            
        </h1>
        <div class="content">
            <h2 id="复杂光照"><a href="#复杂光照" class="headerlink" title="复杂光照"></a><strong>复杂光照</strong></h2><p>在前面学习中，我们场景都仅有一个光源且光源类型上平行光，但在实际的游戏开发过程中，我们往往需要处理数目更多，类型更复杂的光源。比如:点光源，聚光灯</p>
<a id="more"></a>

<h2 id="Unity的渲染路径"><a href="#Unity的渲染路径" class="headerlink" title="Unity的渲染路径"></a><strong>Unity的渲染路径</strong></h2><p><strong>渲染路径决定了光照是如何应用到UnityShader中</strong><br>因此，如果要和光源打交道，我们需要为每个Pass指定它使用的渲染路径，UnityShader才会把正确的光源和处理后的光照信息存放到对应的数据里以便我们访，光照计算才能正确的执行。<br>最新Unity目前主要有2种类型的渲染路径:</p>
<ul>
<li>前向渲染路径(Forward Rendering Path)</li>
<li>延迟渲染路径(Deferred Rendering Path)</li>
</ul>
<p><em>大多数情况，一个项目只使用一种渲染路径</em><br>一般在Unity的GraphicsSetting的TierSettings设置，默认上前向渲染路径。有时，我们希望使用多个渲染路径，A摄像机为Forward，B摄像机为Deferred，我们可以修改camera的渲染路径，以便覆盖Graphics的设置。<br>为每个Pass指定渲染路径，通过<strong>LightMode</strong>标签来实现:</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Pass&#123;</span><br><span class="line">	Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"ForwardBase"</span>&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>LightMode标签支持的渲染路径设置选项</strong></p>
<table>
<thead>
<tr>
<th>标签名</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Always</td>
<td>不管使用哪种渲染路径，该Pass总是被渲染，但不计算任何光照</td>
</tr>
<tr>
<td>ForwadBase</td>
<td>用于前向渲染，该Pass会计算环境光，平行光，逐顶点/SH光源和LightMaps</td>
</tr>
<tr>
<td>ForwadAdd</td>
<td>用于前向渲染，该pass会计算额外的逐像素光源，每个Pass对应一个光源</td>
</tr>
<tr>
<td>Deferred</td>
<td>用于延迟渲染，该Pass会渲染G缓冲</td>
</tr>
<tr>
<td>ShadowCaster</td>
<td>把物体的深度信息渲染到阴影映射纹理ShadowMap或一张深度纹理中</td>
</tr>
<tr>
<td>Vertex</td>
<td>废弃的顶点照明渲染</td>
</tr>
</tbody></table>
<h4 id="前向渲染路径"><a href="#前向渲染路径" class="headerlink" title="前向渲染路径"></a><strong>前向渲染路径</strong></h4><p><strong>前向渲染路径有3种处理光照的方式:</strong></p>
<ul>
<li>逐顶点处理</li>
<li>逐像素处理</li>
<li>球谐函数SH处理</li>
</ul>
<p><strong>而决定一个光源使用哪种处理方式取决于它的类型和渲染模式。</strong></p>
<ul>
<li>光源类型，指的是该光源是平行光还是其它类型的光源</li>
<li>渲染模式，指的是该光源是否是重要的(important),在光源的RenderMode中选择</li>
</ul>
<p><strong>光照模式设置为Important，则该光源会被Unity当成一个逐像素光源来处理</strong></p>
<p>在前向渲染中，当我们渲染一个物体时，Unity会根据场景中各个光源的设置以及这些光源对物体的影响程度(距离该物体的远近，光源强度等)对这些光源进行一个重要度排序。<br>其中，一定数目的光源会按逐像素的方式处理，然后最多有4个光源按逐顶点的方式处理，剩下的光源按SH方式处理。</p>
<ul>
<li>场景中最亮的平行光总是按逐像素处理</li>
<li>渲染模式为Important的光源会按逐像素处理，如果逐像素光源数量小于QualitySetting中设置的数目，会有更多光源以逐像素的方式处理</li>
<li>设置为Not Important的光源会按逐顶点或SH处理</li>
</ul>
<h6 id="ForwardBase"><a href="#ForwardBase" class="headerlink" title="ForwardBase"></a><strong>ForwardBase</strong></h6><p><strong>可实现的光照效果:</strong></p>
<ul>
<li>光照纹理(lightmap)</li>
<li>环境光</li>
<li>自发光</li>
<li>阴影(平行光的阴影)</li>
</ul>
<p><strong>渲染设置</strong></p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Pass&#123;</span><br><span class="line">	Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"ForwardBase"</span>&#125;</span><br><span class="line">	#pragma multi_compile_fwdbase</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>光照计算</strong><br>一个逐像素的平行光以及所有逐顶点和SH光源，不计算其它影响该物体的逐像素光源。</p>
<h6 id="ForwardAdd"><a href="#ForwardAdd" class="headerlink" title="ForwardAdd"></a><strong>ForwardAdd</strong></h6><p><strong>可实现的光照效果:</strong></p>
<ul>
<li>默认情况不支持阴影，但可以通过使用#pragma multi_compile_fwdadd_fullshadows编译指令开启阴影，实现阴影效果</li>
</ul>
<p><strong>渲染设置</strong></p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Pass&#123;</span><br><span class="line">	Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"ForwardAdd"</span>&#125;</span><br><span class="line">	Blend One One</span><br><span class="line">	#pragma multi_compile_fwdadd</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>光照计算</strong><br>其它影响该物体的逐像素光源,每个光源执行一次Pass</p>
<ul>
<li><p>使用multi_complie_编译指令，我们才可以在相关的Pass中得到一些正确的光照变量，例如光照衰减值等。</p>
</li>
<li><p>BasePass中渲染的平行光默认是支持阴影的，而AddPass中渲染的光源默认是没有阴影效果，即便我们在Light组件中设置了阴影的ShadowType。但我们可以使用编译指令multi_compile_fwdadd_fullshadows,为点光源和聚光灯开启阴影效果，不过会使用更多的Shader变体。</p>
</li>
<li><p>环境光和自发光也在BasePass中计算，对一个物体来说，环境光和自发光我们只希望计算一次即可，而在AddPass中计算这2种光，会造成多次叠加。</p>
</li>
<li><p>AddPass渲染设置中开启了Blend，我们希望每个AddPass可以与上一次的光照结果在帧缓存中进行叠加，从而得到多个光照的渲染效果。</p>
</li>
<li><p>一个BasePass仅会执行一次(定义多个BasePass的情况除外),而一个AddPass会根据影响该物体的其它逐像素光源的数目被多次调用，既每个逐像素光源会执行一次AddPass.</p>
</li>
</ul>
<h6 id="内置的光照变量和函数"><a href="#内置的光照变量和函数" class="headerlink" title="内置的光照变量和函数"></a><strong>内置的光照变量和函数</strong></h6><p>我们已经知道，根据使用的渲染路径LightMode，Unity会把不同的光照变量传递个Shader，我们可以在Shader中访问这些光照变量:</p>
<p><strong>前向渲染可以使用的内置光照变量</strong></p>
<table>
<thead>
<tr>
<th>标签名</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>_LightColor0</td>
<td>float4</td>
<td>该Pass处理的逐像素光源的颜色</td>
</tr>
<tr>
<td>_LightTexture0</td>
<td>float4</td>
<td>点光源和聚光灯的衰减纹理，通过光源空间下的顶点坐标采样获取衰减值</td>
</tr>
<tr>
<td>_LightTextureB0</td>
<td>float4</td>
<td>光源是cookie的衰减纹理，通过光源空间下的顶点坐标采样获取衰减值</td>
</tr>
<tr>
<td>_WorldSpaceLightPos0</td>
<td>float4</td>
<td>xyz该Pass处理的逐像素光源的位置。w=0,则该光源为平行光,w=1,为其它光源</td>
</tr>
<tr>
<td>unity_WorldToLight</td>
<td>float4x4</td>
<td>从世界空间到光源空间的变换矩阵，可以用于采样cookie和光照衰减纹理</td>
</tr>
<tr>
<td>unity_4LightPosX0/Y0/Z0</td>
<td>float4</td>
<td>仅用于BasePass，前4个非重要的点光源在世界空间中的位置</td>
</tr>
<tr>
<td>unity_4LightAtten0</td>
<td>float4</td>
<td>仅用于BasePass，前4个非重要的点光源的衰减因子</td>
</tr>
<tr>
<td>unity_LightColor</td>
<td>half4[4]</td>
<td>仅用于BasePass，前4个非重要的点光源的颜色</td>
</tr>
</tbody></table>
<p><strong>前向渲染可以使用的内置函数</strong></p>
<table>
<thead>
<tr>
<th>标签名</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>float3 WorldSpaceLightDir(float4)</td>
<td>仅用于前向渲染，输入一个模型空间的顶点位置，返回世界空间中从该点到光源的光照方向，没有被归一化</td>
</tr>
<tr>
<td>float3 UnityWorldSpaceLightDir(float4)</td>
<td>仅用于前向渲染，输入世界空间点坐标，返回世界空间中光照方向，没有被归一化</td>
</tr>
<tr>
<td>float3 ObjSpaceLightDir(float4)</td>
<td>仅用于前向渲染，输入一个模型空间的顶点位置，返回模型空间中从该点到光源的光照方向，没有被归一化</td>
</tr>
<tr>
<td>float3 Shade4PointLights(…)</td>
<td>仅用于前向渲染，计算4个点光源的光照，用来计算逐顶点光照</td>
</tr>
</tbody></table>
<h4 id="延迟渲染路径"><a href="#延迟渲染路径" class="headerlink" title="延迟渲染路径"></a><strong>延迟渲染路径</strong></h4><p>前向渲染问题:当场景中包含大量实时光源时，前向渲染的性能会急速下降，一个物体被多个光源影响时，每个物体执行多个Pass来计算不同光源对该物体的光照结果，每执行一个Pass都需要重新渲染一遍物体，这会造成重复计算。<br>原理:<br>延迟渲染主要包含2个Pass，第一个Pass不进行任何光照计算，仅仅计算哪些片元可见，并存储到G缓冲区(G-buffer),第二个Pass，利用G-buffer的各个片元信息(表面法线，视角方向,漫反射系数等),进行真正光照。<br><strong>延迟渲染缺点:</strong></p>
<ul>
<li>不支持真正的抗锯齿(anti-aliasing)功能</li>
<li>不能处理半透明物体</li>
<li>对显卡有要求，需要支持MRT，shadermode3.0以上</li>
</ul>
<h2 id="Unity的光源类型"><a href="#Unity的光源类型" class="headerlink" title="Unity的光源类型"></a><strong>Unity的光源类型</strong></h2><ul>
<li>平行光</li>
<li>点光源</li>
<li>聚光灯</li>
<li>面光源(仅烘焙时有效)</li>
</ul>
<p><strong>光源的属性：位置，方向，颜色，强度，衰减</strong></p>
<p>衰减：随着物体逐渐远离光源而逐渐减小，光照强度变弱。</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Shader<span class="hljs-string">"MyTest/Test4/forwardbase"</span>&#123;</span><br><span class="line">	Properties&#123;</span><br><span class="line">		_Diffuse(<span class="hljs-string">"Main Color"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_Specular(<span class="hljs-string">"Specular Color"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_Gloss(<span class="hljs-string">"Gloss"</span>,Range(<span class="hljs-number">8.0</span>,<span class="hljs-number">256</span>))= <span class="hljs-number">20</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	SubShader&#123;</span><br><span class="line">		Tags&#123;<span class="hljs-string">"RenderType"</span>=<span class="hljs-string">"Opaque"</span>&#125;</span><br><span class="line"></span><br><span class="line">		Pass&#123;</span><br><span class="line">			Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"ForwardBase"</span>&#125;</span><br><span class="line">			CGPROGRAM</span><br><span class="line">			#pragma multi_compile_fwdbase</span><br><span class="line"></span><br><span class="line">			#include"Lighting.cginc"</span><br><span class="line">			#pragma vertex vert</span><br><span class="line">			#pragma fragment frag</span><br><span class="line"></span><br><span class="line">			fixed4 _Diffuse;</span><br><span class="line">			fixed4 _Specular;</span><br><span class="line">			float _Gloss;</span><br><span class="line"></span><br><span class="line">			struct a2v&#123;</span><br><span class="line">				float4 vertex:POSITION;</span><br><span class="line">				float3 normal:NORMAL;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			struct v2f&#123;</span><br><span class="line">				float4 pos:SV_POSITION;</span><br><span class="line">				float3 worldNormal:TEXCOORD0;</span><br><span class="line">				float3 worldPos:TEXCOORD1;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			v2f vert(a2v i)&#123;</span><br><span class="line">				v2f o;</span><br><span class="line">				o.pos = UnityObjectToClipPos(i.vertex);</span><br><span class="line">				o.worldNormal = UnityObjectToWorldNormal(i.normal);</span><br><span class="line">				o.worldPos = mul(unity_ObjectToWorld,i.vertex).xyz;</span><br><span class="line">				<span class="hljs-keyword">return</span> o;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			fixed4 frag(v2f v):SV_Target&#123;</span><br><span class="line">				fixed3 worldNormal = normalize(v.worldNormal);</span><br><span class="line">				fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);</span><br><span class="line"></span><br><span class="line">				fixed3 ambinet = UNITY_LIGHTMODEL_AMBIENT.xyz;</span><br><span class="line">				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(<span class="hljs-number">0</span>,dot(worldNormal,worldLight));</span><br><span class="line"></span><br><span class="line">				fixed3 viewdir = normalize(UnityWorldSpaceViewDir(v.worldPos));</span><br><span class="line">				fixed3 halfdir = normalize(viewdir + worldLight );</span><br><span class="line">				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(<span class="hljs-number">0</span>,dot(halfdir,worldNormal)),_Gloss);</span><br><span class="line">				fixed atten = <span class="hljs-number">1.0</span>;</span><br><span class="line"></span><br><span class="line">				fixed3 color = ambinet + (diffuse + specular)*atten;</span><br><span class="line">				<span class="hljs-keyword">return</span> fixed4(color,<span class="hljs-number">1</span>);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			ENDCG</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		Pass&#123;</span><br><span class="line">			Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"ForwardAdd"</span>&#125;</span><br><span class="line">			Blend One One</span><br><span class="line">			CGPROGRAM</span><br><span class="line">			#include "Lighting.cginc"</span><br><span class="line">			#include "AutoLight.cginc"</span><br><span class="line"></span><br><span class="line">			#pragma multi_compile_fwdadd</span><br><span class="line">			#pragma vertex vert</span><br><span class="line">			#pragma fragment frag</span><br><span class="line"></span><br><span class="line">			fixed4 _Diffuse;</span><br><span class="line">			fixed4 _Specular;</span><br><span class="line">			float _Gloss;</span><br><span class="line"></span><br><span class="line">			struct a2v&#123;</span><br><span class="line">				float4 vertex:POSITION;</span><br><span class="line">				float3 normal:NORMAL;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			struct v2f&#123;</span><br><span class="line">				float4 pos:SV_POSITION;</span><br><span class="line">				float3 worldNormal:TEXCOORD0;</span><br><span class="line">				float3 worldPos:TEXCOORD1;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			</span><br><span class="line">			v2f vert(a2v i)&#123;</span><br><span class="line">				v2f o;</span><br><span class="line">				o.pos = UnityObjectToClipPos(i.vertex);</span><br><span class="line">				o.worldNormal = UnityObjectToWorldNormal(i.normal);</span><br><span class="line">				o.worldPos = mul(unity_ObjectToWorld,i.vertex).xyz;</span><br><span class="line">				<span class="hljs-keyword">return</span> o;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			fixed4 frag(v2f v):SV_Target&#123;</span><br><span class="line">				fixed3 worldNormal = normalize(v.worldNormal);</span><br><span class="line">				#if USING_DIRECTIONAL_LIGHT</span><br><span class="line">					fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);</span><br><span class="line">				#else</span><br><span class="line">					fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz - v.worldPos);</span><br><span class="line">				#endif</span><br><span class="line"></span><br><span class="line">				fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb *max(<span class="hljs-number">0</span>,dot(worldLight,worldNormal));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//fixed3 viewdir =  normalize(_WorldSpaceCameraPos.xyz - v.worldPos.xyz);</span></span><br><span class="line">				fixed3 viewdir = normalize(UnityWorldSpaceViewDir(v.worldPos));</span><br><span class="line">				fixed3 halfdir = normalize(viewdir + worldLight);</span><br><span class="line">				fixed3 specular = _LightColor0.rgb *_Specular.rgb *pow(max(<span class="hljs-number">0</span>,dot(halfdir,worldNormal)),_Gloss);</span><br><span class="line"></span><br><span class="line">				#ifdef USING_DIRECTIONAL_LIGHT</span><br><span class="line">					fixed atten = <span class="hljs-number">1.0</span>;</span><br><span class="line">				#else</span><br><span class="line">					</span><br><span class="line">				    float3 lightCoord = mul(unity_WorldToLight, float4(v.worldPos, <span class="hljs-number">1</span>)).xyz;</span><br><span class="line">				    fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;</span><br><span class="line"></span><br><span class="line">				#endif</span><br><span class="line"></span><br><span class="line">				fixed3 color =  (diffuse + specular)*atten;</span><br><span class="line">				<span class="hljs-keyword">return</span> fixed4(color,<span class="hljs-number">1</span>);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			ENDCG</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="光源方向"><a href="#光源方向" class="headerlink" title="光源方向"></a><strong>光源方向</strong></h4><ul>
<li>根据光源类型计算不同光源方向:</li>
</ul>
<figure class="highlight hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#ifdef USING_DIRECTIONAL_LIGHT</span><br><span class="line">	fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);</span><br><span class="line">#else</span><br><span class="line">	fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos.xyz);</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>

<p><strong>USING_DIRECTIONAL_LIGHT:</strong> 当前向渲染Pass处理光源类型是平行光时，底层渲染引擎会定义此宏命令。<br><strong>_WorldSpaceLightPos0.xyz:</strong> 平行光时，光源方向可以直接由xyz得到，点光源和聚光灯，xyz表示是世界空间下的光源位置，想要得到光源方向，需要减去世界空间下的顶点位置。</p>
<h4 id="光源衰减"><a href="#光源衰减" class="headerlink" title="光源衰减"></a><strong>光源衰减</strong></h4><ul>
<li>根据光源类型计算不同光源衰减</li>
</ul>
<figure class="highlight hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#ifdef USING_DIRECTIONAL_LIGHT</span><br><span class="line">	fixed atten = <span class="hljs-number">1.0</span>;</span><br><span class="line">#else</span><br><span class="line">	</span><br><span class="line">	#if defined (POINT)</span><br><span class="line">        float3 lightCoord = mul(unity_WorldToLight, float4(v.worldPos, <span class="hljs-number">1</span>)).xyz;</span><br><span class="line">        fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;</span><br><span class="line">    #elif defined (SPOT)</span><br><span class="line">        float4 lightCoord = mul(unity_WorldToLight, float4(v.worldPos, <span class="hljs-number">1</span>));</span><br><span class="line">        fixed atten = (lightCoord.z &gt; <span class="hljs-number">0</span>) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + <span class="hljs-number">0.5</span>).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;</span><br><span class="line">    #else</span><br><span class="line">        fixed atten = <span class="hljs-number">1.0</span>;</span><br><span class="line">    #endif</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>

<p><strong>POINT:</strong><br>当前向渲染Pass处理的是点光源时，底层会定义此宏</p>
<p><strong>SPOT:</strong><br>当前向渲染Pass处理的是聚光灯时，底层会定义此宏，只有实现此衰减，聚光灯才有效果</p>
<h6 id="光源衰减纹理"><a href="#光源衰减纹理" class="headerlink" title="光源衰减纹理"></a><strong>光源衰减纹理</strong></h6><p>Unity在内部使用了一张名为_LightTexture0的纹理来计算光源衰减，需要注意的是，如果我们对该光源使用了cookie，那么衰减查找的纹理是_LightTextureB0。<br>为了对_LightTexture0纹理采样得到给定点到该光源的衰减值，我们需要得到该点在光源空间中的位置:<br>然后，我们可以使用这个坐标的模的平方对衰减纹理_LightTexture0进行采样，得到衰减值。可以发现，在上面的代码中，我们使用了光源空间中顶点距离的平方（通过dot 函数来得到）来对纹理采样，之所以没有使用距离值来采样是因为这种方法可以避免开方操作。然后，我们使用宏UNITY_ATTEN_CHANNEL来得到衰减纹理中衰减值所在的分量，以得到最终的衰减值。</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">float3 lightCoord = mul(unity_WorldToLight, float4(v.worldPos, <span class="hljs-number">1</span>))</span><br><span class="line"></span><br><span class="line">fixed atten = tex2D（_LightTexture0，dot（lightCoord，lightCoord）.rr）.UNITY_ATTEN_CHANNEL；</span><br></pre></td></tr></table></figure>

<h6 id="数学公式计算衰减"><a href="#数学公式计算衰减" class="headerlink" title="数学公式计算衰减"></a><strong>数学公式计算衰减</strong></h6><p>无法在shader中通过内置变量得到光源的范围，聚光灯的朝向，张开角度等信息，因此得到效果不太好。</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">float dist = length(_WorldSpaceLightPos0.xyz - v.worldPos);</span><br><span class="line">fixed atten = <span class="hljs-number">1.0</span>/dist;</span><br></pre></td></tr></table></figure>

<p><strong>以上Shader因没有在BasePass中计算逐顶点和SH光源，因此无法显示Not Important的光源效果</strong></p>
<h2 id="逐顶点光源"><a href="#逐顶点光源" class="headerlink" title="逐顶点光源"></a><strong>逐顶点光源</strong></h2><p>当光源渲染模式设置为Not Important时，这时我们需要在BasePass中顶点函数里处理，这些点光源和聚光灯,最多支持4个逐顶点光源，多出来的以SH光源处理。</p>
<h4 id="逐顶点光源坐标"><a href="#逐顶点光源坐标" class="headerlink" title="逐顶点光源坐标"></a><strong>逐顶点光源坐标</strong></h4><p>light0 的位置是 float3(lightPosX[0], lightPosY[0], lightPosZ[0])</p>
<p>light1 的位置是 float3(lightPosX[1], lightPosY[1], lightPosZ[1])</p>
<p>light2 的位置是 float3(lightPosX[2], lightPosY[2], lightPosZ[2])</p>
<p>light3 的位置是 float3(lightPosX[3], lightPosY[3], lightPosZ[3])</p>
<h4 id="逐顶点光源光照函数"><a href="#逐顶点光源光照函数" class="headerlink" title="逐顶点光源光照函数"></a><strong>逐顶点光源光照函数</strong></h4><figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">float3 Shade4PointLights (</span><br><span class="line">    float4 lightPosX, float4 lightPosY, float4 lightPosZ,</span><br><span class="line">    float3 lightColor0, float3 lightColor1, float3 lightColor2, float3 lightColor3,</span><br><span class="line">    float4 lightAttenSq,</span><br><span class="line">    float3 worldpos, float3 worldnormal)</span><br></pre></td></tr></table></figure>

<p>返回值：<br>float3 返回的是漫反射计算结果<br>参数：<br>unity_4LightPosX0/Y0/Z0：四个逐顶点光源坐标<br>unity_LightColor[0|1|2|3]:四个逐顶点光源颜色，强度<br>unity_4LightAtten0：逐顶点光源衰减值<br>worldpos:顶点世界坐标<br>worldnormal：世界空间下法线</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">float3 vertexLight = Shade4PointLights(unity_4LightPosX0,unity_4LightPosY0,unity_4LightPosZ0,</span><br><span class="line">					unity_LightColor[<span class="hljs-number">0</span>].rgb,unity_LightColor[<span class="hljs-number">1</span>].rgb,unity_LightColor[<span class="hljs-number">2</span>].rgb,unity_LightColor[<span class="hljs-number">3</span>].rgb,</span><br><span class="line">					unity_4LightAtten0,o.worldPos,o.worldNormal);</span><br><span class="line">o.vertexLighting += vertexLight;</span><br></pre></td></tr></table></figure>

<p>计算得到的逐顶点光源的漫反射结果，在片元函数中使用:</p>
<p>fixed3 color = v.vertexLighting + (diffuse + specular)*atten;<br>return fixed4(color,1);</p>
<p><strong>VERTEXLIGHT_ON: 逐顶点光源定义的宏</strong></p>
<h2 id="SH光源"><a href="#SH光源" class="headerlink" title="SH光源"></a><strong>SH光源</strong></h2><p>float3 shlight = ShadeSH9 (float4(o.worldNormal, 1.0));<br>o.vertexLighting = shlight;</p>

        </div>
        
        <div class="level is-size-7 is-uppercase">
            <div class="level-start">
                <div class="level-item">
                    <span class="is-size-6 has-text-grey has-mr-7">#</span>
                    <a class="has-link-grey -link" href="/mystudy/tags/学习/">学习</a>
                </div>
            </div>
        </div>
        
        

        
        <div class="social-share"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css">
<script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script>
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/mystudy/2020/06/04/shader学习9-阴影/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">shader学习9-阴影</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/mystudy/2020/03/01/shader学习7-透明效果/">
                <span class="level-item">shader学习7-透明效果</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>


</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level">
            <div class="level-item has-text-centered" style="flex-shrink: 1">
                <div>
                    
                        <img class="image is-128x128 has-mb-6" src="/mystudy/images/avatar2.jpg" alt="满头大汉老黄牛">
                    
                    
                    <p class="is-size-4 is-block">
                        满头大汉老黄牛
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        游戏
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>火星</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level is-mobile">
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        文章
                    </p>
                    <p class="title has-text-weight-normal">
                        70
                    </p>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        分类
                    </p>
                    <p class="title has-text-weight-normal">
                        10
                    </p>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        标签
                    </p>
                    <p class="title has-text-weight-normal">
                        3
                    </p>
                </div>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://github.com/koliy/" target="_blank">
                关注我</a>
        </div>
        
        
        <div class="level is-mobile">
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="Github" href="https://github.com/koliy/">
                
                <i class="fab fa-github"></i>
                
            </a>
            
        </div>
        
    </div>
</div>
    
        


    
        
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                分类
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/mystudy/categories/3D数学基础-图形与游戏开发/">
            <span class="level-start">
                <span class="level-item">3D数学基础:图形与游戏开发</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/AI/">
            <span class="level-start">
                <span class="level-item">AI</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">12</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/MySql/">
            <span class="level-start">
                <span class="level-item">MySql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/SurfaceShader/">
            <span class="level-start">
                <span class="level-item">SurfaceShader</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/Unity/">
            <span class="level-start">
                <span class="level-item">Unity</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/git/">
            <span class="level-start">
                <span class="level-item">git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/shade学习/">
            <span class="level-start">
                <span class="level-item">shade学习</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">33</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/xlua/">
            <span class="level-start">
                <span class="level-item">xlua</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/网络同步/">
            <span class="level-start">
                <span class="level-item">网络同步</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">10</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/设计模式/">
            <span class="level-start">
                <span class="level-item">设计模式</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li>
            </ul>
        </div>
    </div>
</div>
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/09/">
                <span class="level-start">
                    <span class="level-item">九月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/07/">
                <span class="level-start">
                    <span class="level-item">七月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">23</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/06/">
                <span class="level-start">
                    <span class="level-item">六月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">17</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2019/09/">
                <span class="level-start">
                    <span class="level-item">九月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2019/08/">
                <span class="level-start">
                    <span class="level-item">八月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">18</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
    
        
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            标签云
        </h3>
        <a href="/mystudy/tags/game/" style="font-size: 15px;">game</a> <a href="/mystudy/tags/git/" style="font-size: 10px;">git</a> <a href="/mystudy/tags/学习/" style="font-size: 20px;">学习</a>
    </div>
</div>

    
    
        <div class="column-right-shadow is-hidden-widescreen is-sticky">
        
            
<div class="card widget" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                目录
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#复杂光照">
        <span class="has-mr-6">1</span>
        <span>复杂光照</span>
        </a></li><li>
        <a class="is-flex" href="#Unity的渲染路径">
        <span class="has-mr-6">2</span>
        <span>Unity的渲染路径</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#前向渲染路径">
        <span class="has-mr-6">2.1</span>
        <span>前向渲染路径</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#ForwardBase">
        <span class="has-mr-6">2.1.1</span>
        <span>ForwardBase</span>
        </a></li><li>
        <a class="is-flex" href="#ForwardAdd">
        <span class="has-mr-6">2.1.2</span>
        <span>ForwardAdd</span>
        </a></li><li>
        <a class="is-flex" href="#内置的光照变量和函数">
        <span class="has-mr-6">2.1.3</span>
        <span>内置的光照变量和函数</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#延迟渲染路径">
        <span class="has-mr-6">2.2</span>
        <span>延迟渲染路径</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#Unity的光源类型">
        <span class="has-mr-6">3</span>
        <span>Unity的光源类型</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#光源方向">
        <span class="has-mr-6">3.1</span>
        <span>光源方向</span>
        </a></li><li>
        <a class="is-flex" href="#光源衰减">
        <span class="has-mr-6">3.2</span>
        <span>光源衰减</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#光源衰减纹理">
        <span class="has-mr-6">3.2.1</span>
        <span>光源衰减纹理</span>
        </a></li><li>
        <a class="is-flex" href="#数学公式计算衰减">
        <span class="has-mr-6">3.2.2</span>
        <span>数学公式计算衰减</span>
        </a></li></ul></li></ul></li><li>
        <a class="is-flex" href="#逐顶点光源">
        <span class="has-mr-6">4</span>
        <span>逐顶点光源</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#逐顶点光源坐标">
        <span class="has-mr-6">4.1</span>
        <span>逐顶点光源坐标</span>
        </a></li><li>
        <a class="is-flex" href="#逐顶点光源光照函数">
        <span class="has-mr-6">4.2</span>
        <span>逐顶点光源光照函数</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#SH光源">
        <span class="has-mr-6">5</span>
        <span>SH光源</span>
        </a></li></ul>
        </div>
    </div>
</div>

        
        </div>
    
</div>

                




<div class="column is-4-tablet is-4-desktop is-3-widescreen is-hidden-touch is-hidden-desktop-only has-order-3 column-right is-sticky">
    
        
<div class="card widget" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                目录
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#复杂光照">
        <span class="has-mr-6">1</span>
        <span>复杂光照</span>
        </a></li><li>
        <a class="is-flex" href="#Unity的渲染路径">
        <span class="has-mr-6">2</span>
        <span>Unity的渲染路径</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#前向渲染路径">
        <span class="has-mr-6">2.1</span>
        <span>前向渲染路径</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#ForwardBase">
        <span class="has-mr-6">2.1.1</span>
        <span>ForwardBase</span>
        </a></li><li>
        <a class="is-flex" href="#ForwardAdd">
        <span class="has-mr-6">2.1.2</span>
        <span>ForwardAdd</span>
        </a></li><li>
        <a class="is-flex" href="#内置的光照变量和函数">
        <span class="has-mr-6">2.1.3</span>
        <span>内置的光照变量和函数</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#延迟渲染路径">
        <span class="has-mr-6">2.2</span>
        <span>延迟渲染路径</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#Unity的光源类型">
        <span class="has-mr-6">3</span>
        <span>Unity的光源类型</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#光源方向">
        <span class="has-mr-6">3.1</span>
        <span>光源方向</span>
        </a></li><li>
        <a class="is-flex" href="#光源衰减">
        <span class="has-mr-6">3.2</span>
        <span>光源衰减</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#光源衰减纹理">
        <span class="has-mr-6">3.2.1</span>
        <span>光源衰减纹理</span>
        </a></li><li>
        <a class="is-flex" href="#数学公式计算衰减">
        <span class="has-mr-6">3.2.2</span>
        <span>数学公式计算衰减</span>
        </a></li></ul></li></ul></li><li>
        <a class="is-flex" href="#逐顶点光源">
        <span class="has-mr-6">4</span>
        <span>逐顶点光源</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#逐顶点光源坐标">
        <span class="has-mr-6">4.1</span>
        <span>逐顶点光源坐标</span>
        </a></li><li>
        <a class="is-flex" href="#逐顶点光源光照函数">
        <span class="has-mr-6">4.2</span>
        <span>逐顶点光源光照函数</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#SH光源">
        <span class="has-mr-6">5</span>
        <span>SH光源</span>
        </a></li></ul>
        </div>
    </div>
</div>

    
    
</div>

            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container1">
        <div class="level1">
            <div class="level-start1 has-text-centered-mobile1">
                <a class="footer-logo is-block has-mb-6" href="/mystudy/">
                

                
                </a>
                <p class="is-size-7">
                &copy; 2020 Koliy&nbsp;
                Powered by <a href="https://github.com/koliy/koliy.github.io" target="_blank">Hexo</a> & <a
                        href="https://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                共<span id="busuanzi_value_site_uv">0</span>个访客
                </span>
                
                </p>
            </div>
            <div class="level-end1">
            
            </div>
        </div>
    </div>
</footer>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("zh-CN");</script>

<script>
var IcarusThemeSettings = {
    article: {
        highlight: {
            clipboard: true,
            fold: 'unfolded'
        }
    }
};
</script>


    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>



    
    
    
    <script src="/mystudy/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/mystudy/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="回到顶端" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/mystudy/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    

    


<script src="/mystudy/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="想要查找什么..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: '文章',
                PAGES: '页面',
                CATEGORIES: '分类',
                TAGS: '标签',
                UNTITLED: '(无标题)',
            },
            CONTENT_URL: '/mystudy/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/mystudy/js/insight.js" defer></script>
<link rel="stylesheet" href="/mystudy/css/search.css">
<link rel="stylesheet" href="/mystudy/css/insight.css"><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->
    
</body>
</html>